package net.noday.base.fanli;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;

import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sun.tools.javac.util.List;

public class Area {

	public static void main(String[] args) throws Exception {
		Document doc = Jsoup.connect("http://d.jd.com/area/get")
				.ignoreContentType(true)
				.data("fid", "0").timeout(10000)
				.get();
		JSONArray arr = JSON.parseArray(doc.body().text());
		JSONArray a2 = new JSONArray();
		for (int i = 0; i < 35; i++) {
			JSONObject o = arr.getJSONObject(i);
			a2.add(o);
			System.out.println("INSERT INTO `tbl_zone` (`id`, `name`, `zipcode`, `pid`, `type`, `code`)"
					+ " VALUES('"+o.getString("id")+"','"+o.getString("name")+"',NULL,'1','20',NULL);");
		}
		JSONArray a3 = new JSONArray();
		for (int i = 0; i < a2.size(); i++) {
			JSONObject o = a2.getJSONObject(i);
			Document doc2 = Jsoup.connect("http://d.jd.com/area/get")
					.ignoreContentType(true)
					.data("fid", o.getString("id")).timeout(10000)
					.get();
			if (StringUtils.isNotBlank(doc2.body().text())) {
				System.out.println(doc2.body().text()+"|"+o.getString("id"));
				JSONArray arr2 = filter(doc2.body().text());
				
				for (int j = 0; j < arr2.size(); j++) {
					JSONObject o2 = arr2.getJSONObject(j);
					a3.add(o2);
					System.out.println("INSERT INTO `tbl_zone` (`id`, `name`, `zipcode`, `pid`, `type`, `code`)"
							+ " VALUES('"+o2.getString("id")+"','"+o2.getString("name")+"',NULL,'"+o.getString("id")+"','30',NULL);");
				}
			}
		}
		JSONArray a4 = new JSONArray();
		for (int i = 0; i < a3.size(); i++) {
			JSONObject o = a3.getJSONObject(i);
			Document doc2 = Jsoup.connect("http://d.jd.com/area/get")
					.ignoreContentType(true)
					.data("fid", o.getString("id")).timeout(10000)
					.get();
			if (StringUtils.isNotBlank(doc2.body().text())) {
				System.out.println(doc2.body().text()+"|"+o.getString("id"));
				JSONArray arr2 = filter(doc2.body().text());
				for (int j = 0; j < arr2.size(); j++) {
					JSONObject o2 = arr2.getJSONObject(j);
					a4.add(o2);
					System.out.println("INSERT INTO `tbl_zone` (`id`, `name`, `zipcode`, `pid`, `type`, `code`)"
							+ " VALUES('"+o2.getString("id")+"','"+o2.getString("name")+"',NULL,'"+o.getString("id")+"','40',NULL);");
				}
			}
		}
		for (int i = 0; i < a4.size(); i++) {
			JSONObject o = a4.getJSONObject(i);
			Document doc2 = Jsoup.connect("http://d.jd.com/area/get")
					.ignoreContentType(true)
					.data("fid", o.getString("id")).timeout(10000)
					.get();
			if (StringUtils.isNotBlank(doc2.body().text())) {
				System.out.println(doc2.body().text()+"|"+o.getString("id"));
				JSONArray arr2 = filter(doc2.body().text());
				for (int j = 0; j < arr2.size(); j++) {
					JSONObject o2 = arr2.getJSONObject(j);
					System.out.println("INSERT INTO `tbl_zone` (`id`, `name`, `zipcode`, `pid`, `type`, `code`)"
							+ " VALUES('"+o2.getString("id")+"','"+o2.getString("name")+"',NULL,'"+o.getString("id")+"','50',NULL);");
				}
			}
		}
	}

	private static JSONArray filter(String a) {
		if (a.startsWith("null(")) {
			return JSON.parseArray(a.substring(5, a.length()-1));
		} if (a.startsWith("{")) {
			System.out.println(a);
			return new JSONArray();
		}
		return JSON.parseArray(a);
	}
}
